package com.interview.controller;

import com.interview.pojo.LoginDto;
import com.interview.service.UserService;
import com.itheima.common.pojo.Result;
import com.itheima.common.util.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userLogin")
public class UserController {

    @Autowired
    private UserService userService;

    //前端的校验的工作（手机） validation 专门做数据校验的框架
    @PostMapping("/login")
    public Result<Map> login(@RequestBody LoginDto loginDto) throws Exception {

        //2.如果不是 则需要判断 手机号和密码是否为空 为空返回错误
        if (StringUtils.isEmpty(loginDto.getUsername()) || StringUtils.isEmpty(loginDto.getPassword())) {
            return Result.errorMessage("错误的用户名或者密码");
        }

        //3.执行登录的业务逻辑生成真实的令牌
        Map<String, Object> info = userService.login(loginDto);

        return Result.ok(info);

    }

}
